package org.ala.distributed_transaction_commons.redis;


/**
 * redis key管理
 *
 * @author 骆毅(ala)
 * @date 2024年2月18日
 */
public class KeyNamespace {
	
	
	/**
	 * 统一过期时间（单位：秒）
	 * <p>	半小时
	 */
	public final static long TIMEOUT = 1800l;

	
	/**
	 * 事务环境id模版
	 * %s[0] = biz
	 * %s[1] = 事务id
	 */
	final static String TPL_TRANSACTION_ID = "hash:distributed_transaction:context:%s:%s";
	/**
	 * 取事务环境redis id
	 */
	public static String transactionContextId(String biz, String txId) {
		return String.format(TPL_TRANSACTION_ID, biz, txId);
	}
	/**
	 * 事务环境hash feild
	 */
	public final static String FIELD_CONTEXT_ID = "txId",
								FIELD_CONTEXT_BIZ = "biz",
								FIELD_CONTEXT_STATUS = "status",
								FIELD_CONTEXT_TYPE = "type",
								FIELD_CONTEXT_START_TIMESTAMP = "startTimestamp",
								FIELD_CONTEXT_START_DATE = "startDate",
								FIELD_CONTEXT_MIN_PARTICIPANT_COUNT_BEGIN = "minParticipantCountBegin",
								FIELD_CONTEXT_MIN_PARTICIPANT_COUNT_CAN_COMMIT = "minParticipantCountCanCommit",
								FIELD_CONTEXT_MIN_PARTICIPANT_COUNT_PRE_COMMIT = "minParticipantCountPreCommit",
								FIELD_CONTEXT_MIN_PARTICIPANT_COUNT_COMMIT = "minParticipantCountCommit";
	
	
	/**
	 * 参与者集合id模版
	 * %s[0] = biz
	 * %s[1] = 事务id
	 * value = 参与者id
	 */
	final static String TPL_PARTICIPANT_SET_ID = "set:distributed_transaction:participant_set:%s:%s";
	/**
	 * 取参与者集合redis id
	 */
	public static String transactionParticipantSetIds(String biz, String txId) {
		return String.format(TPL_PARTICIPANT_SET_ID, biz, txId);
	}
	
	
	/**
	 * 参与者id模版
	 * %s[0] = biz
	 * %s[1] = 参与者id
	 */
	final static String TPL_PARTICIPANT_ID = "hash:distributed_transaction:participant:%s:%s";
	/**
	 * 取参与者redis id
	 */
	public static String transactionParticipantId(String biz, String pid) {
		return String.format(TPL_PARTICIPANT_ID, biz, pid);
	}
	/**
	 * 事务参与者hash feild
	 */
	public final static String FIELD_PARTICIPANT_TXID = "txId",
								FIELD_PARTICIPANT_BIZ = "biz",
								FIELD_PARTICIPANT_ID = "participantId",
								FIELD_PARTICIPANT_ROLE = "role",
								FIELD_PARTICIPANT_STATUS = "status",
								FIELD_PARTICIPANT_TYPE = "type",
								FIELD_PARTICIPANT_START_TIMESTAMP = "startTimestamp",
								FIELD_PARTICIPANT_START_DATE = "startDate";
}
